package com.kingre.et.action;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.context.SecurityContextHolder;

import com.kingre.et.Constants;
import com.kingre.et.model.Role;
import com.kingre.et.model.School;
import com.kingre.et.model.User;
import com.kingre.et.service.IUserService;
import com.opensymphony.xwork2.ActionSupport;

public class LoginRedirectAction extends ActionSupport {
    private static final long serialVersionUID = -1208961823830520525L;
    private static final Logger logger = Logger.getLogger(LoginRedirectAction.class);
    private String roleName;
    private School school;
    @Autowired
    private IUserService userService;

    public String loginSuccess(){
        User currentUser = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        logger.debug("user.name =" + currentUser.getName());
        logger.debug("user.email =" + currentUser.getEmail());
        Role role = currentUser.getRole();
        logger.debug("user.role =" + role);
        if(!role.getName().equals(Constants.USER_ROLE)){
           roleName = SecurityContextHolder.getContext().getAuthentication().getAuthorities()[0].getAuthority();
           if(roleName.equals(Constants.TEACHER_ROLE))
               school = userService.findSchool(currentUser.getId());
           return "manager";
        }
        if(userService.isQuestionFinish(currentUser.getId()))
            return "display";
        return "answer";
    }
    public String getRoleName() {
        return roleName;
    }
    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }
    public School getSchool() {
        return school;
    }
    public void setSchool(School school) {
        this.school = school;
    }
}

